package com.pyue.eam.regi.controller;

import cn.hutool.core.collection.CollUtil;
import com.aizuda.limiter.annotation.RateLimit;
import com.aizuda.limiter.strategy.IpKeyGenerateStrategy;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pyue.eam.admin.api.constant.ResponseWord;
import com.pyue.eam.admin.api.entity.regi.CqTaskPlanEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.common.core.util.R;
import com.pyue.eam.common.log.annotation.SysLog;
import com.pyue.eam.regi.service.CqTaskPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务方案
 *
 * @date 2023-12-09 20:06:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cqTaskPlan")
@Tag(description = "cqTaskPlan", name = "任务方案管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CqTaskPlanController {
    private final CqTaskPlanService cqTaskPlanService;

    /**
     * 通过任务id获取 任务方案
     *
     * @param taskId 任务id
     */
    @GetMapping("/listByTaskId/{taskId}")
    public R listByTaskId(@PathVariable("taskId") Long taskId) {
        List<CqTaskPlanEntity> list = cqTaskPlanService.list(Wrappers.lambdaQuery(CqTaskPlanEntity.class)
                .eq(CqTaskPlanEntity::getTaskId, taskId));
        // chargeAvatar

        // if (CollUtil.isNotEmpty(list)) {
        //     list.forEach(e->{
        //         e.setTacticsTrans("记时");
        //         e.setChargeAvatar("/admin/sys-file/sh-sys-cloud/1eceeccea5eb4e5981c905583b70b93b.jpeg");
        //     });
        // }
        return R.ok(list);
    }

    /**
     * 通过id查询任务方案
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(cqTaskPlanService.getById(id));
    }

    /**
     * 新增任务方案
     *
     * @param cqTaskPlan 任务方案
     * @return R
     */
    @Operation(summary = "新增任务方案", description = "新增任务方案")
    @SysLog("新增任务方案")
    @PostMapping
    @RateLimit(count = 1, interval = "3s", strategy = {IpKeyGenerateStrategy.TYPE}, message = ResponseWord.WAIT_TIME)
    public R save(@RequestBody CqTaskPlanEntity cqTaskPlan) {
        return R.ok(cqTaskPlanService.saveTaskPlan(cqTaskPlan));
    }

    /**
     * 修改任务方案
     *
     * @param cqTaskPlan 任务方案
     * @return R
     */
    @Operation(summary = "修改任务方案", description = "修改任务方案")
    @SysLog("修改任务方案")
    @PutMapping
    public R updateById(@RequestBody CqTaskPlanEntity cqTaskPlan) {
        return R.ok(cqTaskPlanService.updateDataById(cqTaskPlan));
    }

    /**
     * 通过id删除任务方案
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除任务方案", description = "通过id删除任务方案")
    @SysLog("通过id删除任务方案")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(cqTaskPlanService.removeBatchByIds(CollUtil.toList(ids)));
    }

}
